package com.dg11185.nearshop.activity.user.order;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.libs.utils.string.StringUtils;
import com.dg11185.nearshop.activity.SubPageActivity;
import com.dg11185.nearshop.app.ViewUtils;
import com.dg11185.nearshop.net.entity.Address;

public class AddressModifyActivity extends SubPageActivity {

    public static String ADD = "add";

    public static String MODIFY = "modify";

    private TextView tvName, tvPhone, tvProvince, tvCity, tvAddress, tvPost;

    private CheckBox cbDefault;

    private Address address = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_address_modify);
        tvName = (TextView) this.findViewById(R.id.address_name);
        tvPhone = (TextView) this.findViewById(R.id.address_phone);
        tvProvince = (TextView) this.findViewById(R.id.address_province);
        tvCity = (TextView) this.findViewById(R.id.address_city);
        tvAddress = (TextView) this.findViewById(R.id.address_detail);
        tvPost = (TextView) this.findViewById(R.id.address_post);
        cbDefault = (CheckBox) this.findViewById(R.id.address_default);

        initIntentData();
    }

    @Override
    protected void onDestroy() {
        NetClient.httpCancel(this, true);
        super.onDestroy();
    }

    private void initIntentData() {
        String tag = getIntent().getStringExtra("tag");
        
        if(MODIFY.equals(tag)){
            address = getIntent().getExtras().getParcelable("address_entity");
            tvName.setText(address.name);
            tvPhone.setText(address.phone);
            tvProvince.setText(address.province);
            tvCity.setText(address.city);
            tvAddress.setText(address.address);
            tvPost.setText(address.postCode);
        }else{
            address = new Address();
        }
    }

    public void onSubmitClicked(View v) {
        if (StringUtils.isBlank(tvName.getText().toString())) {
            ViewUtils.showToast(this, "收件人姓名不能为空..");
            return;
        }
        if (StringUtils.isBlank(tvPhone.getText().toString())) {
            ViewUtils.showToast(this, "手机号不能为空..");
            return;
        }
        if (StringUtils.isBlank(tvProvince.getText().toString())) {
            ViewUtils.showToast(this, "请选择省份..");
            return;
        }
        if (StringUtils.isBlank(tvName.getText().toString())) {
            ViewUtils.showToast(this, "请选择城市");
            return;
        }
        if (StringUtils.isBlank(tvName.getText().toString())) {
            ViewUtils.showToast(this, "请输入详细收货地址...");
            return;
        }
        if (StringUtils.isBlank(tvName.getText().toString())) {
            ViewUtils.showToast(this, "请填写邮编..");
            return;
        }

        address.name = tvName.getText().toString();
        address.phone = tvPhone.getText().toString();
        address.province = tvProvince.getText().toString();
        address.city = tvCity.getText().toString();
        address.address = tvAddress.getText().toString();
        address.postCode = tvPost.getText().toString();
        // TODO 提交到数据库
    }

}
